<?php

namespace app\controllers;

use Yii;
use app\tools\Re;
use app\models\Area;
use app\models\User;
use app\models\Good;
use app\models\OrderInfo;
use app\models\OrderInfoGood;
use app\models\Address;
use app\models\ShoppingCart;
use app\models\UserHistory;

class UserController extends BaseController
{
    public $state_arr = ['待付款', '待发货', '待收货', '售后中', '已完成', '退款中', '退款成功', '退款失败', '已取消'];
    public $audit_status_name_arr = ['待审核', '审核成功', '审核失败'];

    //用户信息
    public function actionUserInfo(){
      $openid = Yii::$app->request->post('openid', '');
      $user_info = User::find()->where(['openid' => $openid, 'status' => 1])->one();
      if (!$user_info) {
        return Re::data(null, '用户信息读取错误', Re::ERROR);
      }
      $data = [
        'id' => $user_info->id,
        'nickname' => $user_info->nickname,
        'name' => $user_info->nickname,
        'phone' => $user_info->phone,
        'openid' => $user_info->openid,
        'icon' => $user_info->icon,
        'first_invitation_name' => $user_info->invitation_id ? $user_info->invitation->name : ''
      ];
      return Re::data(['data' => $data]);
    }

    //个人中心相关数据
    public function actionUserCenterData(){
      $openid = Yii::$app->request->post('openid', '');
      $user_info = User::find()->where(['openid' => $openid, 'status' => 1])->one();
      if (!$user_info) {
        return Re::data(null, '用户信息读取错误', Re::ERROR);
      }
      $order_dfk_count = $order_query = OrderInfo::find()->where(['user_id' => $user_info->id, 'order_type' => 1, 'state' => 0])->count();//待付款
      $order_dfh_count = $order_query = OrderInfo::find()->where(['user_id' => $user_info->id, 'order_type' => 1, 'state' => 1])->count();//待发货
      $order_dsh_count = $order_query = OrderInfo::find()->where(['user_id' => $user_info->id, 'order_type' => 1, 'state' => 2])->count();//待收货
      $order_sh_count = $order_query = OrderInfo::find()->where(['user_id' => $user_info->id, 'order_type' => 1, 'state' => 5])->count();//售后
      $user_cart_count = ShoppingCart::find()->where(['user_id' => $user_info->id, 'status' => 1])->count();
      $data = [
        'order_dfk_count' => $order_dfk_count,
        'order_dfh_count' => $order_dfh_count,
        'order_dsh_count' => $order_dsh_count,
        'order_sh_count' => $order_sh_count,
        'user_cart_count' => $user_cart_count,
      ];
      return Re::data(['data' => $data]);
    }

    //个人地址列表
    public function actionUserAddress(){
      $openid = Yii::$app->request->post('openid', '');
      $user_info = User::find()->where(['openid' => $openid, 'status' => 1])->one();
      if (!$user_info) {
        return Re::data(null, '用户信息读取错误', Re::ERROR);
      }
      $address = Address::find()->where(['user_id' => $user_info->id])->orderBy("default desc, created_at desc")->all();
      $address_data = [];
      if ($address) {
        foreach ($address as $v) {
          $address_data[] = [
            'id' => $v->id,
            'name' => $v->name,
            'phone' => $v->phone,
            'province_name' => $v->area->parentArea->parentArea->name,
            'city_name' => $v->area->parentArea->name,
            'area_name' => $v->area->name,
            'province_id' => $v->area->parentArea->parentArea->id,
            'city_id' => $v->area->parentArea->id,
            'area_id' => $v->area_id,
            'address' => $v->area->parentArea->parentArea->name.$v->area->parentArea->name.$v->area->name.$v->address,
            'default' => $v->default,
          ];
        }
      }
      return Re::data(['data' => $address_data]);
    }

    //设置默认地址
    public function actionSetDefaultAddress(){
      $id = Yii::$app->request->post('id', '');
      $openid = Yii::$app->request->post('openid', '');
      $user_info = User::find()->where(['openid' => $openid, 'status' => 1])->one();
      if (!$user_info) {
        return Re::data(null, '用户信息读取错误', Re::ERROR);
      }
      $address = Address::findOne($id);
      if (!$address) {
        return Re::data(null, '地址信息读取错误', Re::ERROR);
      }
      Address::updateAll(['default' => 0], ['user_id' => $user_info->id]);
      $address->default = 1;
      $address->save();
      return Re::data();
    }

    //地址删除
    public function actionDeleteAddress(){
      $id = Yii::$app->request->post('id', '');
      $address = Address::findOne($id);
      if (!$address) {
        return Re::data(null, '地址信息读取错误', Re::ERROR);
      }
      $address->delete();
      return Re::data();
    }

    //地址详情
    public function actionAddressInfo(){
      $id = Yii::$app->request->post('id', '');
      $address = Address::findOne($id);
      if (!$address) {
        return Re::data(null, '地址信息读取错误', Re::ERROR);
      }
      $address_data = [
        'id' => $address->id,
        'name' => $address->name,
        'phone' => $address->phone,
        'province_name' => $address->area->parentArea->parentArea->name,
        'city_name' => $address->area->parentArea->name,
        'area_name' => $address->area->name,
        'province_id' => $address->area->parentArea->parentArea->id,
        'city_id' => $address->area->parentArea->id,
        'area_id' => $address->area_id,
        'address' => $address->address,
        'default' => $address->default,
      ];
      return Re::data(['data' => $address_data]);
    }

    //新增/修改地址
    public function actionEditAddress(){
      $id = Yii::$app->request->post('id', 0);
      $name = Yii::$app->request->post('name', '');
      $phone = Yii::$app->request->post('phone', '');
      $area_name = Yii::$app->request->post('area_name', 0);
      $address = Yii::$app->request->post('address', '');
      $default = Yii::$app->request->post('default', 0);
      $openid = Yii::$app->request->post('openid', '');
      $user_info = User::find()->where(['openid' => $openid, 'status' => 1])->one();
      if (!$user_info) {
        return Re::data(null, '用户信息读取错误', Re::ERROR);
      }
      $area_name_arr = explode('-', $area_name);
      $city_info = Area::find()->where(['name' => $area_name_arr[1], 'level' => 1])->one();
      if ($city_info) {
        $area_info = Area::find()->where(['name' => $area_name_arr[2], 'parent_id' => $city_info->id, 'level' => 2])->one();
        if ($area_info) {
          $area_id = $area_info->id;
        } else {
          $area_info = new Area();
          $area_info->parent_id = $city_info->id;
          $area_info->level = 2;
          $area_info->name = $area_name_arr[2];
          $area_info->short_name = $area_name_arr[2];
          $area_info->save();
          $area_id = $area_info->id;
        }
      }
      if ($id) {
        $address_info = Address::findOne($id);
      } else {
        $address_info = new Address();
      }
      if ($default) {
        Address::updateAll(['default' => 0], ['user_id' => $user_info->id]);
      }
      $address_info->user_id = $user_info->id;
      $address_info->name = $name;
      $address_info->phone = $phone;
      $address_info->area_id = $area_id;
      $address_info->address = $address;
      $address_info->default = $default;
      $address_info->created_at = time();
      if (!$address_info->save()) {var_dump($address->getErrors());die;
        return Re::data(null, '操作失败', Re::ERROR);
      }
      return Re::data();
    }

    //我的足迹
    public function actionUserHistory(){
      $openid = Yii::$app->request->post('openid', '');
      $page = Yii::$app->request->post('page', 0);
      $page_size = Yii::$app->request->post('page_size', 10);
      $user_info = User::find()->where(['openid' => $openid, 'status' => 1])->one();
      if (!$user_info) {
        return Re::data(null, '用户信息读取错误', Re::ERROR);
      }
      $user_history_list = UserHistory::find()->where(['user_id' => $user_info->id])->orderBy('created_at desc')->limit($page_size)->offset($page_size*$page)->all();;
      $user_history_data = [];
      if ($user_history_list) {
        foreach ($user_history_list as $v) {
          $user_history_data[] = [
            'good_id' => $v->good->id,
            'good_name' => $v->good->name,
            'good_img' => $v->good->img,
            'created_time' => date('Y-m-d H:i:s', $v->created_at)
          ];
        }
      }
      return Re::data(['data' => $user_history_data]);
    }

    //清空我的足迹
    public function actionClearUserHistory(){
      $openid = Yii::$app->request->post('openid', '');
      $user_info = User::find()->where(['openid' => $openid, 'status' => 1])->one();
      if (!$user_info) {
        return Re::data(null, '用户信息读取错误', Re::ERROR);
      }
      UserHistory::deleteAll(['user_id' => $user_info->id]);
      return Re::data();
    }
}
